import { Component, Node } from "cc";

export class NodeUI {

    private map: Map<string, Node> = new Map();

    constructor(node: Node) {
        const traverse = (currentNode: Node) => {
            this.map.set(currentNode.name, currentNode);
            for (const child of currentNode.children) {
                traverse(child);
            }
        };
        traverse(node);
    }

    node(nodeName: string): Node {
        return this.map.get(nodeName);
    }

    component<T extends Component>(nodeName: string, component: new (...args: any[]) => T): T {
        return this.map.get(nodeName)?.getComponent(component);
    }

}

